var util = new NT.utilObj.util();
var mainNavContent = {};
var loginUserName;
var loginUserRealName;
var loginUserRoleIds;
var msgInfos;
$(function() {
	loadTree();
	loadLoginUserInfo();
	//loadTips();
	$("#logout").click(
			function(){
				logoutSys();
			}
	);
	function logoutSys(){
		if(confirm("是否确认登出系统？")){
			util.emmAjax({
				url : './logOut.action',
				loading:false,
				success : function(data) {
					var obj = eval('(' + data + ')');
					if(obj.success=="true"){
						window.location.href = './';
					}
				}
			});
		}
	}
	$("#changeUserPwd").click(function(){
		resetAddOrEditForm();
		$("#action").html('密码修改');
		$("#addOrEditModal").modal("show");
		updatePwd(loginUserName);
	});
	dropdownOpen();
	
	
	
});
clearAddOrEidtPsw=function(){
	check.clear("oldPwd");
	check.clear("newPwd");
	check.clear("againPwd");
};

/**
 * 鼠标划过就展开子菜单，免得需要点击才能展开
 */
function dropdownOpen() {
 
    var $dropdownLi = $('#userDropDown');
    $dropdownLi.mouseover(function() {
        $(this).addClass('open');
    }).mouseout(function() {
        $(this).removeClass('open');
    });
   
}

/**
 * 清空form
 */
var resetAddOrEditForm = function(){
	$("#addOrEditForm")[0].reset();
	clearAddOrEidtPsw();
};
/**
 *加载菜单树
 */
function loadTree() {
	var util = new NT.utilObj.util();
	util.emmAjax({
		url : './menu/menuTreeByUser.action',
		success : function(data) {
			var obj = eval('(' + data + ')');
			initRootMenu(obj.data.children);
		}
	});
};

/**
 *获取登录用户名
 */
function loadLoginUserInfo() {
	var util = new NT.utilObj.util();
	util.emmAjax({
		url : './login/getLoginUserInfo.action',
		success : function(data) {
			var obj = eval('(' + data + ')');
			loginUserName = obj.data.userName;
			loginUserRealName = obj.data.realName;
			loginUserRoleIds=obj.data.roleIds;
			$("#getUserName1").html("<i class='glyphicon glyphicon-user'></i>&nbsp;"+loginUserRealName+"<b class='caret'></b>");
			//$("#getUserName1").html("<span class='glyphicon glyphicon-user'> "+loginUserRealName+"<b class='caret'></b></span>");
			loadTips();
		}
	});
};

/**
 *获取登录用户名
 */
function loadTips() {
	var util = new NT.utilObj.util();
	util.emmAjax({
		url : './msgInfo/findByCondition.action',
		success : function(data) {
			var obj = eval('(' + data + ')');
			msgInfos=obj.data;
			var tipnums=obj.number;
			if(tipnums!=null&&tipnums!='0')
			{
				$("#tipnums").html(tipnums);
			}
			else
			{
				$("#tipnums").html("");
			}
			/*if(msgInfos!=null)
			{
				$("#tipnums").html(tipnums);
				var htmls="";
				for(var i=0;i<msgInfos.length;i++)
				{
					var userName = obj.data[i].userName;
					var loanIds = obj.data[i].loanId;
					var msg = obj.data[i].msg;
					var ids = obj.data[i].id;
					
					htmls+='<li><a><span id="'+ids+'" class="" onclick="showOneTip(\''+loanIds+'\')" style="cursor:pointer;">'+msg+'</span></a></li>';
	                
				}
				htmls+='<li class="divider"></li>'+
						'<li><a><span id="" onclick="showAllTip()" style="cursor:pointer;">&nbsp;查看全部</span></a></li>';
				//alert(htmls);
				$("#tipsMenu").html(htmls);
			}
			else
				{
					$("#tipnums").html("");
					$("#tipsMenu").hide();
				}*/
		}
	});
};

/*function showAllTip(){

		
		if(isInArray2(loginUserRoleIds,1010))
		{
			//客户经理
			$('#subIframe').attr("src", "loanAppCommit/loanAppCommit.html");
		}
		else if(isInArray2(loginUserRoleIds,1011))
		{
			//贷款审查分配人
			$('#subIframe').attr("src", "loanAppDistribute/loanAppDistribute.html");
		}
		else if(isInArray2(loginUserRoleIds,1012))
		{
			//独立审查人
			$('#subIframe').attr("src", "loanAppExamine/loanAppExamine.html");
		}
		else if(isInArray2(loginUserRoleIds,1013))
		{
			//贷款上报人
			$('#subIframe').attr("src", "loanAppCommitConfirm/loanAppCommitConfirm.html");
		}
		
		deleteMsgAll();
		
		
};*/

function isInArray2(arr,value){
    var index = $.inArray(value,arr);
    if(index >= 0){
        return true;
    }
    return false;
}

/*function showOneTip(loanId){
	showModel();
	//editDataFill(loanId);
	//单个删除
	//deleteMsg(loanId);
};*/

/* $(function () { $('#myModal').on('hide.bs.modal', function () {
	 alert('嘿，我听说您喜欢模态框...');})
	 });*/

/*function deleteMsg(id) {
	var util = new NT.utilObj.util();
	util.emmAjax({
		url : './msgInfo/delMsgInfo.action',
		data:{'loanId':id},
		type : 'post',
		success : function(data) {
			var obj = util.str2Json(data);
			//var object=obj.data.;
			if(obj.success=='true')
			{
				loadTips();
			}
			else{
				util.sysAlert(obj.data);
			}
		}
	});
};*/

function deleteMsgAll() {
	var util = new NT.utilObj.util();
	util.emmAjax({
		url : './msgInfo/delMsgInfos.action',
		data:{'userName':loginUserName},
		type : 'post',
		success : function(data) {
			var obj = util.str2Json(data);
			//var object=obj.data.;
			if(obj.success=='true')
			{
				loadTips();
			}
			else{
				util.sysAlert(obj.data);
			}
		}
	});
};

/**
 * 显示模态框
 */
var  showModel = function(){
	if(msgInfos!=null&&msgInfos.length>0)
	{
		fillTipModal();
		$("#oneTipModal").modal("show");
	}
};

function fillTipModal()
{
	var htmls="";
	for(var i=0;i<msgInfos.length;i++)
	{
		htmls+="<tr><td class='propertyName'><i class='glyphicon glyphicon-list'></i>&nbsp;"+msgInfos[i].msg+"</td></tr>";
	}
	$("#tipTable").html(htmls);
	
};

/**
 *初始化一级菜单
 */
function initRootMenu(treeJsonData) {
	for ( var i = 0; i < treeJsonData.length; i++) {
		var rootUlHtml = '	<li id="menuId_'+treeJsonData[i].id+'" contentname="'+treeJsonData[i].text+'">'
						+'		<a href="#">'
						+'			<i class="fa '+treeJsonData[i].iconText+'" style="font-size:20px;"></i>'
						+'			<span class="nav-label">'+treeJsonData[i].text+'</span>'
						+'			<span class="fa arrow"></span>'
						+'		</a>'
						+'	</li>';
		$('#demo-list').append(rootUlHtml);
		
		var menuChildren = treeJsonData[i].children;
		generateNextMenu(menuChildren);
	}
	jQuery("#jquery-accordion-menu").jqueryAccordionMenu();
};

/**
 *生成二级菜单
 */

function generateNextMenu(subchildren){
	if(subchildren==null||subchildren.length<1){
		return;
	}
	var menuUlHtml = '<ul class="submenu" id="menuNextId_'+subchildren[0].parentId+'"></ul>';
	$('#menuId_'+subchildren[0].parentId).append(menuUlHtml);
	for(var i=0;i<subchildren.length;i++){
		var menuLiHtml = '<li onclick="loadIframe(this)" parentid="menuId_'+subchildren[0].parentId+'" frmaeUrl="'+subchildren[i].urlText+'"  ><a name="subchildrenText" href="#">'+subchildren[i].text+'</a></li>';
		$('#menuNextId_'+subchildren[i].parentId).append(menuLiHtml);
	}
};


/**
 *加载子页面
 */
function loadIframe(dom) {
	
	$("a[name='subchildrenText']").removeClass("clickMenu");
	$($(dom).find("a")[0]).addClass("clickMenu");
	
	
	var url = dom.getAttribute('frmaeUrl');
	var parentId = dom.getAttribute('parentid');
	var parentName = $("#"+parentId).attr("contentname");
	var thisName = $($(dom).find("a")[0]).html();
	mainNavContent["parentName"] = parentName;
	mainNavContent["thisName"] = thisName;
	$('#subIframe').attr("src", url);
};
function backMain() {
	$('#subIframe').attr("src", "./logo.html");
}

var querySession = function(){
	global_filePushResInterval = setInterval(function(){
		util.emmAjax({
			url : './user/querySession.action',
			loading:false,
			success : function(data) {
				var data = util.str2Json(data);
				if(data.success=="false"){
					window.location.href = './logOut.action';
				}
			}
		});
	},20000);
};

